//
//  ESRecomendTViewController.m
//  Eyeshot
//
//  Created by lanou3g on 15/7/8.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESRecomendTViewController.h"
#import "ESTagsListCell.h"
@interface ESRecomendTViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *recommendTTableView;

@property (strong,nonatomic) NSMutableArray *recommendTagsArray;//列表数组

@end

@implementation ESRecomendTViewController

- (void)viewWillAppear:(BOOL)animated {
    self.view.backgroundColor = [UIColor colorWithRed: 234/255.0 green:234/255.0 blue: 234/255.0 alpha:1.0];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self createRecommendTTableView];
}

#pragma mark -请求数据
- (void)requestData{
    //1.获得请求管理者
    AFHTTPRequestOperationManager *netManager = [[AFHTTPRequestOperationManager alloc] init];
    //2.设置参数
    
    //3.发送GET请求
    [netManager GET:@"http://api.budejie.com/api/api_open.php?a=tag_recommend&action=sub&appname=baisishequ&asid=1880212C-B451-4FA7-8DFA-E07254EFCEE9&c=topic&client=iphone&device=iPhone%204S&jbk=0&limit=50&mac=&market=&openudid=5a299ed728ffa000f0c4db0eb49e93d0e3968b2a&type=0&udid=&ver=3.6" parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *dic) {
        //1.推荐标签列表数据
        _recommendTagsArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dic1 in dic) {
            ESTagsListModel *model = [[ESTagsListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [_recommendTagsArray addObject:model];
        }
        //刷新表格
        [_recommendTTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"数据请求失败:%@",error);
    }];
}

#pragma mark - 创建表视图

- (void)createRecommendTTableView{
    //1.设置表代理
    _recommendTTableView.dataSource = self;
    _recommendTTableView.delegate = self;
    //2.注册cell类
    [_recommendTTableView registerNib:[UINib nibWithNibName:@"ESTagsListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"tagsListCell"];
}
#pragma mark - 实现表视图的协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recommendTagsArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseIdentifer = @"tagsListCell";
    ESTagsListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
    if (cell == nil) {
        cell = [[ESTagsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
    }
    cell.tagsListModel = _recommendTagsArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

@end
